module servertable;

private {
	import dwt.all;
	import serverlist;
	import launch;
	import main;
	import common;
}

// should correspond to serverlist.ServerColumn
char[][] serverHeaders = ["Name", "Ping", "Players", "Game", "Map", "IP"];

class ServerTable
{
	/*************************************************
	               PUBLIC METHODS
	*************************************************/
	this(Composite parent) {
		parent_ = parent;
		table_ = new Table(parent, DWT.VIRTUAL | DWT.MULTI | DWT.FULL_SELECTION | DWT.BORDER);
		table_.setHeaderVisible(true);
		table_.setLinesVisible(true);

		for (int i = 0; i < serverHeaders.length; i++) {
			TableColumn column = new TableColumn(table_, DWT.NONE);
			column.setText(serverHeaders[i]);
		}

		table_.getColumn(0).setWidth(250);
		table_.getColumn(1).setWidth(40);
		table_.getColumn(2).setWidth(50);
		table_.getColumn(3).setWidth(50);
		table_.getColumn(4).setWidth(90);
		table_.getColumn(5).setWidth(130);

		table_.addListener(DWT.SetData, new class Listener {
			public void handleEvent(Event e) {
				TableItem item = cast(TableItem) e.item;
				item.setText(serverList.getFiltered(table_.indexOf(item)).server);
			}
		});

		table_.addSelectionListener(new class SelectionAdapter {
			void widgetSelected(SelectionEvent e) {
				playerTable.setSelectedServers(table_.getSelectionIndices());
				cvarTable.setItems(serverList.getFiltered(table_.getSelectionIndex()).cvars);
			}
			void widgetDefaultSelected(SelectionEvent e) {
				JoinServer(serverList.getFiltered(table_.getSelectionIndex()).server[ServerColumn.ADDRESS]);
			}
		});

		Listener sortListener = new class Listener {
			public void handleEvent(Event e) {
				// determine new sort column and direction
				TableColumn sortColumn, newColumn;
				int dir;

				sortColumn = table_.getSortColumn();
				newColumn = cast(TableColumn) e.widget;
				dir = table_.getSortDirection();

				if (sortColumn is newColumn) {
					dir = (dir == DWT.UP) ? DWT.DOWN : DWT.UP;
				} else {
					dir = DWT.UP;
					table_.setSortColumn(newColumn);
					serverList.sortColumn = table_.indexOf(newColumn);
				}

				// sort the data based on column and direction
				if (dir == DWT.UP) {
					serverList.reversed = false;
					serverList.sort();
				}
				else {
					serverList.reversed = true;
					serverList.sort();
				}

				table_.setSortDirection(dir);
				table_.setItemCount(serverList.filteredLength());
				table_.clearAll();

			}
		};

		for (int i = 0; i < table_.getColumnCount(); i++) {
			TableColumn c;
			c = table_.getColumn(i);
			c.addListener(DWT.Selection, sortListener);
		}

		table_.setSortColumn(table_.getColumn(0));
		table_.setSortDirection(DWT.UP);
	}

	Table getTable() { return table_; };


	void update(Object dummy = null) {
		if (!table_.isDisposed) {
			size_t len = serverList.filteredLength;
			table_.setItemCount(len);
		}
	}

	void reset(Object dummy = null) {
		if(!table_.isDisposed()) {
			refresh();
			if (!parselist.abort) {
				setDefaultStatus();
			}
			if (table_.getItemCount() > 0 && table_.getSelectionIndex == -1) {
				table_.setSelection(0);
				playerTable.setSelectedServers(table_.getSelectionIndices());
				cvarTable.setItems(serverList.getFiltered(table_.getSelectionIndex()).cvars);
			}
		}
	}

	void refresh(Object dummy = null) {
		if(!table_.isDisposed()) {
			table_.clearAll();
			table_.setItemCount(serverList.filteredLength);
		}
	}

	void setDefaultStatus()	{
		if (serverList.filteredLength != serverList.length) {
			statusBar.setLeft("Showing " ~
			         std.string.toString(serverList.filteredLength) ~ " of " ~
			         std.string.toString(serverList.length) ~ " servers");
		}
		else {
			statusBar.setLeft("Showing " ~ std.string.toString(serverList.length) ~
			         " servers");
		}
	}

	/************************************************
	            PRIVATE STUFF
	 ************************************************/
	private Table table_;
	private Composite parent_;
}
